<?php

class ModelAbstractAbstract extends Model{
	
	public function addAbstract($data){
		
		$sql = sprintf("
				INSERT INTO 
					abstract
				SET
					title = '%s',
					content = '%s',
					session = '%s',
					customer_id = '%d'
				",
				$this->db->escape($data['title']),
				$this->db->escape($data['content']),
				$this->db->escape($data['session']),
				(int)$this->customer->getId()
				);
		$this->db->query($sql);
		$abstract_id = $this->db->getLastId();
		return $abstract_id;
	}
	
	public function getAbstract($abstract_id){
		
		$sql = sprintf("
				SELECT 
					* 
				FROM 
					abstract
				WHERE
					abstract_id = '%d' 
				AND
					customer_id = '%d'
				",
				(int)$abstract_id,
				(int)$this->customer->getId()
			);
		$abstract_data = $this->db->query($sql);
		
		if($abstract_data->num_rows){
			return $abstract_data->row;
		}else{
			return false;
		}
	}
	
	public function editAbstract($abstract_id, $data){
		
		$sql = sprintf("
				UPDATE 
					abstract
				SET
					title = '%s',
					content = '%s',
					session = '%s'
				WHERE
					abstract_id = '%d'
				AND
					customer_id = '%d'
				",
				$this->db->escape($data['title']),
				$this->db->escape($data['content']),
				$this->db->escape($data['session']),
				(int)$abstract_id,
				(int)$this->customer->getId()
				);
		$this->db->query($sql);
		return $abstract_id;
	}
	
	public function deleteAbstract($abstract_id){
		
		$sql = sprintf("
				DELETE FROM 
					abstract
				WHERE 
					abstract_id = '%d'
				AND
					customer_id = '%d'
				",
				(int)$abstract_id,
				(int)$this->customer->getId()
				);
		$this->db->query($sql);
		return $abstract_id;
	}
	
	
	public function getAbstracts($customer_id){
		
		$abstract_data = array();
		
		$sql = sprintf("
				SELECT 
					*
				FROM 
					abstract
				WHERE 
					customer_id = %d 
				ORDER BY abstract_id",
				$customer_id
				);
		
		$query = $this->db->query($sql);
		/*
		foreach ($query->rows as $result){
			$abstract_data[] = array(
					'abstract_id'	=> $result['abstract_id'],
					'title'			=> $result['title'],
					'content'		=> $result['content'],
					'session'		=> $result['session'],
					'customer_id'	=> $result['customer_id']
			);
		}
		*/
		return $query->rows;
	}
	
}